import { createRouter, createWebHistory } from 'vue-router'
import HomeView from '@/views/Home/home.vue'
import IndexView from '@/views/Home/index.vue'
import ClaView from '@/views/Home/cla.vue'
import CartView from '@/views/Home/cart.vue'
import MyView from '@/views/Home/my.vue'
import registerView from '@/views/Register/index.vue'
import loginView from '@/views/Login/index.vue'
import goodsView from '@/views/Goods/index.vue'
import detailView from '@/views/Detail/index.vue'
import orderdetailView from '@/views/Detail/detaildata.vue'
import orderlistView from '@/views/Orderlist/index.vue'
import SearchView from '@/views/Search/index.vue'
const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    // 重定向
    { path: '/', redirect: '/home/index' },
    { path: '/Search', name: 'Search', component: SearchView },
    {
      path: '/home', component: HomeView, children: [
        { path: 'index', component: IndexView },
        { path: 'cla', component: ClaView },
        { path: 'cart', component: CartView },
        { path: 'my', component: MyView },
      ]
    },
    // 注册
    { path: '/register', name: 'register', component: registerView },
    // 登录
    { path: '/login', name: 'login', component: loginView },
    // 商品列表
    { path: '/goods/:query', name: 'goods', component: goodsView },
    // 商品详情
    { path: '/detail/:goods_id', name: 'detail', component: detailView },
    // 订单详情
    { path: '/orderdetail/:orderId', name: 'orderdetail', component: orderdetailView },
    { path: '/Orderlist', name: 'Orderlist', component: orderlistView },

  ],
})
//导航守卫
router.beforeEach((to, from, next) => {
  let token = localStorage.getItem('token')
  if (to.fullPath == '/home/cart' || to.fullPath == '/home/my') {
    if (!token) {
      next('/login')
    } else {
      // token
      next()
    }
  } else {
    next()
  }
})
export default router
